package com.project.lk.nurseapp.common.view.bluetooth;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.common.adapter.BaseRecycleViewAdapter;
import com.project.lk.nurseapp.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能介绍：这是蓝牙搜索列表的adapter
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class BluetoothItemAdapter extends BaseRecycleViewAdapter {

    List<BlueTooth> mList = new ArrayList<>();
    Context mContext;

    public void setmList(List<BlueTooth> list){
        for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
            for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                if  (list.get(j).getAddress().equals(list.get(i).getAddress()))  {
                    list.remove(j);
                }
            }
        }
        this.mList=list;
    }
    public BluetoothItemAdapter(List<BlueTooth> list, Context context) {
        this.mList = list;
        this.mContext = context;
    }

    public List<BlueTooth> getList(){
        return mList;
    }
    public interface ItemEventListenter{
        void conntItem(int position);
    }
    ItemEventListenter mListenter;
    public void setOnItemEvent(ItemEventListenter listenter){
        this.mListenter=listenter;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bluetooth_layout, parent, false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.itemAddress.setText(mList.get(position).getAddress());
        Log.e("MLOG",mList.get(position).getName()+">>>>>>>>>>>>>>");
        viewHolder.itemName.setText(mList.get(position).getName());
        if (mList.get(position).getAddress().contains("JKFR")){
            boolean thermom = (boolean) SharedPreferencesUtils.getParam("Thermom", false);
            if (thermom) {
                viewHolder.itemHint.setVisibility(View.VISIBLE);
            }else {
                viewHolder.itemHint.setVisibility(View.GONE);
            }
        }else if (mList.get(position).getAddress().contains("SZLSD SPPLE Module")){
            boolean sphy = (boolean) SharedPreferencesUtils.getParam("Sphy", false);
            if (sphy) {
                viewHolder.itemHint.setVisibility(View.VISIBLE);
            }else {
                viewHolder.itemHint.setVisibility(View.GONE);
            }
        }else if (mList.get(position).getAddress().contains("JKBGM")){
            boolean glucose = (boolean) SharedPreferencesUtils.getParam("Glucose", false);
            if (glucose) {
                viewHolder.itemHint.setVisibility(View.VISIBLE);
            }else {
                viewHolder.itemHint.setVisibility(View.GONE);
            }
        }else {
            viewHolder.itemHint.setVisibility(View.GONE);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListenter.conntItem(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_name)
        TextView itemName;
        @BindView(R.id.item_address)
        TextView itemAddress;
        @BindView(R.id.item_hint)
        TextView itemHint;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
